package chatRoom1;

import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class ChatSender implements Runnable {
    private  DatagramSocket client;
    private int targetPort;

    public ChatSender(DatagramSocket client, int targetPort) {
        this.client = client;
        this.targetPort = targetPort;
    }

    @Override
    public void run() {
        try {
            Scanner scanner=new Scanner(System.in);
            while (true){
                String data=scanner.nextLine();
                byte[] buf=data.getBytes(StandardCharsets.UTF_8);
                DatagramPacket packet=new DatagramPacket(buf,0,buf.length,
                        InetAddress.getByName("127.0.0.255"),targetPort);
                client.send(packet);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
